class TimeUtils {
  /// Converts a duration in seconds to a formatted string (HH:MM:SS).
  static String formatDuration(int totalSeconds) {
    final hours = totalSeconds ~/ 3600;
    final minutes = (totalSeconds % 3600) ~/ 60;
    final seconds = totalSeconds % 60;

    final hoursStr = hours.toString().padLeft(2, '0');
    final minutesStr = minutes.toString().padLeft(2, '0');
    final secondsStr = seconds.toString().padLeft(2, '0');

    return '$hoursStr:$minutesStr:$secondsStr';
  }

  static DateTime getMonthStart() {
    final now = DateTime.now();
    return DateTime(now.year, now.month, 1);
  }

  static DateTime getMonthEnd() {
    final now = DateTime.now();
    final nextMonth = (now.month == 12) ? 1 : now.month + 1;
    final nextMonthYear = (now.month == 12) ? now.year + 1 : now.year;
    return DateTime(nextMonthYear, nextMonth, 1)
        .subtract(const Duration(days: 1));
  }

  // 工具函数：安全解析日期
  static DateTime? parseDate(dynamic value) {
    if (value == null) return null;
    if (value is String && value.isNotEmpty) {
      return DateTime.tryParse(value);
    }
    return null;
  }

  // 工具函数：安全序列化日期
  static String? formatDate(DateTime? date) {
    return date?.toIso8601String();
  }
}
